#!/bin/bash
GITDIR="/var/lib/jenkins/workspace"
PRONAME="t_yyg"
DSTDIR="/opt/deploy"
#DATE="`date +%F_%H-%M`"
USER="www"
cat > /home/www/ip.list <<EOF
10.2.11.237
10.2.11.233
EOF
function deploy(){
       cd ${GITDIR}/${PRONAME} && git pull origin tag ${Tag} && git checkout ${Tag}
       cd $GITDIR && tar zcf ${PRONAME}_${Tag}.tar.gz  ${PRONAME}/*
       mv ${PRONAME}_${Tag}.tar.gz /opt/deploy/
       for IP in `cat /home/www/ip.list`;do
                scp /opt/deploy/${PRONAME}_${Tag}.tar.gz ${USER}@${IP}:${DSTDIR}
                ssh ${USER}@${IP} "cd /opt/deploy && tar xf ${PRONAME}_${Tag}.tar.gz && mv ${PRONAME} ${PRONAME}_${Tag}"
                ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${Tag} /var/www/html/${PRONAME}"
                ssh ${USER}@${IP} "chown -R www.www /var/www/html/${PRONAME}"
       done
}
function rollback(){
	  for IP in `cat /home/www/ip.list`;do
                ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${Tag} /var/www/html/${PRONAME}"
                ssh ${USER}@${IP} "chown -R www.www /var/www/html/${PRONAME}"
      done
}

case $Operation in

Deploy)
     deploy
     ;;
Rollback)
     rollback
     ;;
esac
